﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyPhongMach.Model;
using QuanLyPhongMach.Context;
using QuanLyPhongMach.Platform;

namespace QuanLyPhongMach.View
{
    /// <summary>
    /// Interaction logic for WBenhNhan.xaml
    /// </summary>
    public partial class WBenhNhan : Page
    {
        #region VARIABLE

        private CBenhNhan benhnhan;
        private CBenhNhanContext benhnhanContext;
        bool gioitinh;

        #endregion

        #region Function

        public WBenhNhan()
        {
            InitializeComponent();
            TrangThaiMacDinh();
            InitializeFeature();
        }

        private void InitializeFeature()
        {
            benhnhanContext = new CBenhNhanContext(CConstant.CONNECTION);
            try
            {
                cbGioiTinh.Items.Add("Nam");
                cbGioiTinh.Items.Add("Nữ");
                cbGioiTinh.SelectedIndex = 0;
                TaoBangDuLieu();
                CapNhatDuLieuBang();
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "BENHNHAN";

                GridViewColumn mabenhnhan = new GridViewColumn();
                mabenhnhan.DisplayMemberBinding = new Binding("MaBenhNhan");
                mabenhnhan.Header = "Mã Bệnh Nhân";
                mabenhnhan.Width = 120;
                grdView.Columns.Add(mabenhnhan);

                GridViewColumn hoten = new GridViewColumn();
                hoten.DisplayMemberBinding = new Binding("HoTen");
                hoten.Header = "Họ Tên";
                hoten.Width = 120;
                grdView.Columns.Add(hoten);

                GridViewColumn gioitinh = new GridViewColumn();
                gioitinh.DisplayMemberBinding = new Binding("StringGioiTinh");
                gioitinh.Header = "Giới Tính";
                gioitinh.Width = 120;
                grdView.Columns.Add(gioitinh);

                GridViewColumn namsinh = new GridViewColumn();
                namsinh.DisplayMemberBinding = new Binding("ShortNamSinh");
                namsinh.Header = "Năm Sinh";
                namsinh.Width = 120;
                grdView.Columns.Add(namsinh);

                GridViewColumn diachi = new GridViewColumn();
                diachi.DisplayMemberBinding = new Binding("DiaChi");
                diachi.Header = "Địa Chỉ";
                diachi.Width = 300;
                grdView.Columns.Add(diachi);

                lvBenhNhan.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvBenhNhan.ItemsSource = benhnhanContext.LayDanhSachTatCa();
        }

        private void ChonBenhNhan()
        {
            try
            {
                TrangThaiCapNhat();
                if (lvBenhNhan.SelectedItem is CBenhNhan)
                {
                    txtMaBenhNhan.Text = (lvBenhNhan.SelectedItem as CBenhNhan).MaBenhNhan.ToString();
                    txtHoTen.Text = (lvBenhNhan.SelectedItem as CBenhNhan).HoTen;
                    if ((lvBenhNhan.SelectedItem as CBenhNhan).GioiTinh) cbGioiTinh.SelectedIndex = 0;
                    else cbGioiTinh.SelectedIndex = 1;
                    dpNgaySinh.Text = (lvBenhNhan.SelectedItem as CBenhNhan).NamSinh.ToShortDateString();
                    dpNgaySinh.DisplayDate = (lvBenhNhan.SelectedItem as CBenhNhan).NamSinh;
                    txtDiaChi.Text = (lvBenhNhan.SelectedItem as CBenhNhan).DiaChi;
                }
            }
            catch { }
        }

        private void XoaBenhNhan()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    benhnhanContext.Xoa(Convert.ToInt32(txtMaBenhNhan.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();
                    
                    MessageBox.Show("Xóa Thành Công");
                    
                    
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
            
            
        }

        private void LuuBenhNhan()
        {
            try
            {
                if (KiemTraNhap())
                {
                    if (cbGioiTinh.SelectedIndex == 0)
                        gioitinh = true;
                    else gioitinh = false;

                    if (Convert.ToInt32(txtMaBenhNhan.Text) < benhnhanContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            benhnhan = new CBenhNhan(int.Parse(txtMaBenhNhan.Text),txtHoTen.Text, gioitinh, Convert.ToDateTime(dpNgaySinh.DisplayDate), txtDiaChi.Text);
                            benhnhanContext.CapNhat(benhnhan);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiCapNhat();

                    }
                    else
                    {
                        benhnhan = new CBenhNhan(txtHoTen.Text, gioitinh, Convert.ToDateTime(dpNgaySinh.SelectedDate.Value.ToString()), txtDiaChi.Text);
                        benhnhanContext.Them(benhnhan);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiLuu();
                    }

                    CapNhatDuLieuBang();
                }
                

            }
            catch { MessageBox.Show("Chưa Lưu Được\nVui Lòng Kiểm Tra Lại Thông Tin Nhập"); }
            
            
        }

        private void ThemMoiBenhNhan()
        {
            try
            {
                TrangThaiThem();
                txtHoTen.Clear();
                txtMaBenhNhan.Text = benhnhanContext.LayMaSoMoi().ToString();
                dpNgaySinh.SelectedDate = DateTime.Now;
                txtDiaChi.Clear();
            }
            catch { }
        }

        private void TrangThaiMacDinh()
        {
            btXoa.IsEnabled = false;
            btLuu.IsEnabled = false;
            txtHoTen.IsEnabled = false;
            txtMaBenhNhan.IsEnabled = false;
            cbGioiTinh.IsEnabled = false;
            dpNgaySinh.IsEnabled = false;
            txtDiaChi.IsEnabled = false;
            dpNgaySinh.SelectedDate = DateTime.Now;
            txtHoTen.Clear();
            txtDiaChi.Clear();
            txtMaBenhNhan.Clear();
          
        }

        private void TrangThaiThem()
        {
            txtDiaChi.IsEnabled = true;
            txtHoTen.IsEnabled = true;
            dpNgaySinh.IsEnabled = true;
            cbGioiTinh.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;
            txtHoTen.Focus();
        }

        private void TrangThaiLuu()
        {
            txtHoTen.Clear();
            cbGioiTinh.SelectedIndex = 0;
            txtDiaChi.Clear();
            txtMaBenhNhan.Text = benhnhanContext.LayMaSoMoi().ToString();
            txtHoTen.Focus();
            dpNgaySinh.SelectedDate = DateTime.Now;

        }

        private void TrangThaiCapNhat()
        {
            dpNgaySinh.SelectedDate = DateTime.Now;
            txtDiaChi.IsEnabled = true;
            txtHoTen.IsEnabled = true;
            dpNgaySinh.IsEnabled = true;
            cbGioiTinh.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (dpNgaySinh.SelectedDate > DateTime.Now)
            {
                MessageBox.Show("Vui Lòng Kiểm Tra Lại Ngày Sinh");
                dpNgaySinh.Focus();
                return false;
            }
            if (txtHoTen.Text == "" || txtDiaChi.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Đầy Đủ Thông Tin");
                return false;
            }
            return true;
        }

        public void Refresh()
        {
            benhnhanContext.LamMoi();
        }

        #endregion
       
        #region EVEN

        private void btThem_Click(object sender, RoutedEventArgs e)
        {
            ThemMoiBenhNhan();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuBenhNhan();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaBenhNhan();
        }

        private void lvBenhNhan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonBenhNhan();
        }

        private void txtDiaChi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuBenhNhan();
            }
        }

        #endregion

    }
}
